#ifndef _PROCESS_POOL_HPP
#define _pROCESS_POOL_HPP

#include <iostream>
#include <vector>
#include <unistd.h>
#include <string>
#include <cstdlib>
#include "Task.hpp"
#include <sys/wait.h>

// 先描述
class Channel
{
public:
    Channel(int wfd, pid_t subid)
        : _wfd(wfd), _subid(subid)
    {
        _name = "channel-" + std::to_string(_wfd) + "-" + std::to_string(_subid);
    }

    ~Channel()
    {
    }

    std::string name() { return _name; }

    void Send(int taskcode)
    {
        int n = write(_wfd, &taskcode, sizeof(taskcode));
    }

    void Close()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t n = waitpid(_subid,nullptr,0);
        (void)n;
    }

private:
    int _wfd;
    pid_t _subid;
    std::string _name;
};

// 再组织
class ChannelManager
{
public:
    ChannelManager() : _next(0)
    {
    }
    ~ChannelManager() {}

    void Insert(int wfd, pid_t subpid)
    {
        Channel cc(wfd, subpid);
        _channels.push_back(cc);
    }

    void Print()
    {
        for (auto e : _channels)
        {
            std::cout << e.name() << std::endl;
        }
    }

    Channel &Select()
    {

        auto &c = _channels[_next];
        _next++;
        _next %= _channels.size();
        return c;
    }

    void StopSubProcess()
    {
        for(auto& channel : _channels)
        {
            channel.Close();
            std::cout << "关闭：" << channel.name() << std::endl;
        }
    }

    void WaitSubProcess()
    {
        for(auto& channel : _channels)
        {
            channel.Wait();
            std::cout << "回收：" << channel.name() << std::endl;
        }
    }

private:
    std::vector<Channel> _channels;
    int _next;
};

class ProcessPool
{
public:
    ProcessPool(int n)
        : _process_num(n)
    {
        _task.Rigister(PrintLog);
        _task.Rigister(Download);
        _task.Rigister(Upload);
    }
    ~ProcessPool() {}

    void Work(int rfd)
    {
        while (1)
        {
            int code = 0;
            int n = read(rfd, &code, sizeof(code));
            if (n > 0)
            {
                if (n != sizeof(code))
                {
                    continue;
                }
                std::cout << "子进程[" << getpid() << "]<收到任务码：" << code << std::endl;
                _task.Execute(code);
            }
            else if (n == 0)
            {
                std::cout << "子进程结束" << std::endl;
                break;
            }
            else
            {
                std::cout << "读取错误" << std::endl;
                break;
            }
            sleep(1);
        }
    }

    bool Sarter()
    {
        for (int i = 0; i < _process_num; i++)
        {
            //  1.创建管道
            int pipefd[2];
            int n = pipe(pipefd);
            if (n < 0)
            {
                return false;
            }
            else
            {
                pid_t subpid = fork();

                if (subpid < 0)
                {
                    return false;
                }
                else if (subpid == 0)
                {
                    // 2.子进程关闭写端
                    close(pipefd[1]);
                    Work(pipefd[0]);
                    close(pipefd[0]);
                    exit(0);
                }
                else
                {
                    // 父进程关闭读端
                    close(pipefd[0]);

                    _cm.Insert(pipefd[1], subpid);
                }
            }
        }
        return true;
    }

    void Dbug()
    {
        _cm.Print();
    }

    void Run()
    {
        int taskcode = _task.Code();
        auto &c = _cm.Select();
        std::cout << "选择一个子进程" << c.name() << std::endl;
        c.Send(taskcode);
        std::cout << "发送一个任务码" << taskcode << std::endl;
    }

    void Stop()
    {
        _cm.StopSubProcess();
        _cm.WaitSubProcess();
    }

private:
    ChannelManager _cm;
    int _process_num;
    Task _task;
};

#endif